package com.thermofisher.dsc.amanda.common.util;

import com.thermofisher.dsc.amanda.security.model.SecurityUserDetails;
import java.util.List;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * Created by wenjie.yang on 1/3/2019.
 */
public class SessionUtil {
  public static SecurityUserDetails getUserDetails() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return (SecurityUserDetails) authentication.getPrincipal();
  }

  public static String getUserName() {
    return getUserDetails().getUsername();
  }

  public static Long getUserId() {
    return getUserDetails().getId();
  }

  public static List<String> get() {
    return getUserDetails().getPermissions();
  }

  public static boolean hashPermission(String permissionName) {
    UserDetails userDetails = SessionUtil.getUserDetails();
    return (userDetails.getAuthorities().stream()
        .anyMatch(s -> s.getAuthority().equals(permissionName)));
  }




}
